package com.jinduplan.planmanager.ui.fragment

import android.content.Intent
import android.os.Build
import android.view.View
import android.view.ViewGroup
import com.cxz.kotlin.baselibs.base.BaseActivity
import com.cxz.kotlin.baselibs.base.BaseMvpFragment
import com.cxz.kotlin.baselibs.rx.bus.BusEvent
import com.cxz.kotlin.baselibs.rx.bus.RxBus
import com.cxz.kotlin.baselibs.utils.Preference
import com.cxz.kotlin.baselibs.utils.StatusBarUtil
import com.cxz.kotlin.baselibs.utils.UserInfoUtil
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.constant.Constant
import com.jinduplan.planmanager.mvp.contract.MineFragmentContract
import com.jinduplan.planmanager.mvp.presenter.MineFragmentPresenter
import com.jinduplan.planmanager.ui.activity.clockin.ClockinRecordActivity
import com.jinduplan.planmanager.ui.activity.project.ProjectActivity
import com.jinduplan.planmanager.ui.activity.report.ReportTypeListAcitivity
import com.jinduplan.planmanager.ui.activity.user.PersonalInfoActivity
import com.jinduplan.planmanager.utils.GlideUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_mine.*
import kotlinx.android.synthetic.main.fragment_mine.tv_location

class MineFragment : BaseMvpFragment<MineFragmentContract.View, MineFragmentContract.Presenter>(),
    MineFragmentContract.View, View.OnClickListener {

    var mProjiectName: String by Preference(Constant.PROJECT_NAME, "")
    private var mDisposable: Disposable? = null
    companion object {
        fun newInstance(): MineFragment = MineFragment()
    }

    override fun createPresenter(): MineFragmentContract.Presenter = MineFragmentPresenter()

    override fun attachLayoutRes(): Int {
        return R.layout.fragment_mine
    }

    override fun lazyLoad() {
        iv_setting.setOnClickListener {
            (activity as BaseActivity).goActivity(PersonalInfoActivity::class.java)
        }
    }

    override fun initView(view: View) {
        super.initView(view)

        if (Build.VERSION.SDK_INT >= 23) {
            val toolbarLayoutParams: ViewGroup.LayoutParams = rlParent.getLayoutParams()
            rlParent.setPadding(0, StatusBarUtil.getStatusBarHeight(requireActivity()), 0, 0)
            rlParent.setLayoutParams(toolbarLayoutParams)
        }
        if (!UserInfoUtil.avatar.isEmpty()){
            GlideUtil.load(UserInfoUtil.avatar,iv_head)
        }
        relativeLayout2.setOnClickListener(this)
        relativeLayout4.setOnClickListener(this)
        relativeLayout6.setOnClickListener(this)
        val title =  when (UserInfoUtil.labour_type) {
            "1"->{
                "劳工"
            }
            "2"->{
                "班组长"
            }
            "3"->{
                "项目经理"
            }
            else->{
                ""
            }
        }
        tv_position_name.text = if (UserInfoUtil.position.isNullOrEmpty()) title else UserInfoUtil.position
        if (UserInfoUtil.nick_name.isEmpty()){
            tv_user_name.text = UserInfoUtil.userName
        }else{
            tv_user_name.text = UserInfoUtil.nick_name
        }
        tv_score.text = "总评分 ${UserInfoUtil.score}"

        mDisposable = RxBus.get().toFlowable(BusEvent.ProjectEvent::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                tv_location.text = it.projectName

            }
        if (!mProjiectName.isNullOrBlank()) {
            tv_location.setText(mProjiectName)
        }
    }

    override fun onClick(v: View?) {

        when (v?.id) {
            R.id.relativeLayout2 -> {//项目汇总
                startActivity(Intent(activity, ProjectActivity::class.java))
            }
            R.id.relativeLayout4 -> {//廉政举报
                startActivity(Intent(activity, ClockinRecordActivity::class.java))
            }
            R.id.relativeLayout6 -> {//廉政举报
                startActivity(Intent(activity, ReportTypeListAcitivity::class.java))
            }

        }

    }

}